<?php
namespace app\index\controller;
use think\Controller;

class Callback extends Controller
{
	public function index()
	{
		//支付回调
        $file = './log.txt';
        $notifyData = file_get_contents($file);
        $notifyData .= file_get_contents("php://input");
        file_put_contents($file, $notifyData);

        if ($_POST['trade_status'] == 'TRADE_SUCCESS') {

            $order = db('order',[],false)->where('or_no',$_POST['out_trade_no'])->find();

            if ($order['or_pay_status'] == 1) {
                //支付成功
                $set['or_pay_status']  = 2;
                $set['or_pay_time']    = time();
                $set['or_pay_phone']   = $_POST['buyer_logon_id'];
                $set['or_notify_code'] = $_POST['trade_status'];

                $res = db('order',[],false)->where('or_id',$order['or_id'])->update($set);

                //更新商品状态
                $setGoods['gd_re_status'] = 2;
                db('goods',[],false)->where('gd_id',$order['or_pay_gid'])->update($setGoods);

                if ($res) {
                    return "success";
                } else {
                    return "fail";
                }
            }
        } else {
            return "fail";
        }
	}

}